#include "recboxitem.h"
#include "ui_recboxitem.h"
#include <QPropertyAnimation>

RecBoxItem::RecBoxItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RecBoxItem)
{
    ui->setupUi(this);
    // 安装事件拦截器，事件才能拦截成功
    ui->musicImageBox->installEventFilter(this);
}

RecBoxItem::~RecBoxItem()
{
    delete ui;
}

bool RecBoxItem::eventFilter(QObject *watched, QEvent *event)
{
    // 只拦截label
    if (watched == ui->musicImageBox)
    {
        // 拦截鼠标进入和退出
        if (event->type() == QEvent::Enter)
        {
            // 设置动画效果，局部作用一次动画，不需要挂对象树
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox, "geometry");
            animation->setDuration(150);
            animation->setStartValue(QRect(0, 9, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation->setEndValue(QRect(0, 0, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation->start();

            // 动画结束释放
            connect(animation, &QPropertyAnimation::finished, this, [=](){
                delete animation;
            });
        }
        else if (event->type() == QEvent::Leave)
        {
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox, "geometry");
            animation->setDuration(150);
            animation->setStartValue(QRect(0, 0, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation->setEndValue(QRect(0, 9, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation->start();

            // 动画结束释放
            connect(animation, &QPropertyAnimation::finished, this, [=](){
                delete animation;
            });
        }

        return true;
    }

    return QObject::eventFilter(watched, event);
}

void RecBoxItem::setRecImage(const QString &image)
{
    QString style = "border-image: url("+image+");";
    ui->recMusicImage->setStyleSheet(style);
}

void RecBoxItem::setRecText(const QString &text)
{
    ui->recBoxItemText->setText(text);
}
